﻿/****************************** Module Header ******************************\
 * Module Name:  Pagger.cs
 * Module Version:  1.0.0 (2012-9-19)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to process the FormPagger.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Windows.Forms;

namespace UCF.Core.Forms.CustomControls.Pagger
{
    public delegate object GetDataSource(out int recordcount, out int pagecount, string sql, int pageid, int pagesize);
    public partial class SoPagger : UserControl, IPagger
    {
        /// <summary>
        /// 外部分页处理方法(解决WCF资源占用问题)
        /// <![CDATA[
        /// formPagger1.PagingHandler = (int pageid, int pagesize) =>
        /// {
        ///    int recordcount, pagecount;
        ///    dgvList.DataSource = SRC.Current.Call_GetCallRecordPagingList(out recordcount, out pagecount, "", pageid, pagesize);
        ///    SRC.Close();
        ///    formPagger1.Config(recordcount, pagecount, pageid, pagesize);
        /// };
        /// formPagger1.PagingHandler(1, 20);
        /// ]]>
        /// </summary>
        public Action<int, int> PagingHandler { get; set; }
        public SoPagger()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 第一页页码
        /// </summary>
        public int FirstPage { get { return 1; } }

        /// <summary>
        /// 上一页页码
        /// </summary>
        public int PrevPage
        {
            get
            {
                return (this.PageIndex > 1) ? this.PageIndex - 1 : this.PageIndex;
            }
        }

        /// <summary>
        /// 下一页页码
        /// </summary>
        public int NextPage
        {
            get
            {
                return (this.PageIndex < this.PageCount) ? this.PageIndex + 1 : this.PageIndex;
            }
        }

        /// <summary>
        /// 最末页页码
        /// </summary>
        public int LastPage { get { return this.PageCount; } }

        /// <summary>
        /// 当前页页码
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 当前页列表中游标位置
        /// </summary>
        public int ListIndex { get; private set; }

        /// <summary>
        /// 记录集中游标位置
        /// </summary>
        public int RecordIndex { get; private set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount { get; private set; }

        /// <summary>
        /// 分页尺寸
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount { get; private set; }

        /// <summary>
        /// 当前页记录数
        /// </summary>
        public int ListCount { get; private set; }


        public bool Next()
        {
            throw new NotImplementedException();
        }
        private GetDataSource fnc;
        private DataGridView dataGridView;
        private string sql;
        public void BindControl(DataGridView dgv, GetDataSource getDataSource, string sql = "", int pageid = 1, int pagesize = 20)
        {
            if (sql == null) return;
            this.dataGridView = dgv;
            this.fnc = getDataSource;
            this.sql = sql;

            this.Paging(pageid, pagesize);
        }
        public void Paging(int pageid, int pagesize)
        {
            if (PagingHandler != null)
            {
                PagingHandler(pageid, pagesize);
            }
            else
            {
                if (this.sql == null) return;
                int recordcount, pagecount;
                this.dataGridView.DataSource = this.fnc(out recordcount, out pagecount, this.sql, pageid, pagesize);
                this.PageCount = pagecount;
                this.RecordCount = recordcount;
            }

            this.PageIndex = pageid;
            this.PageSize = pagesize;

            lblPageIndex.Text = this.PageIndex.ToString();
            lblPageCount.Text = this.PageCount.ToString();
            lblRecordCount.Text = this.RecordCount.ToString();
            tbPageIndex.Text = this.PageIndex.ToString();
            tbPageSize.Text = this.PageSize.ToString();
        }
        public void Config(int recordcount, int pagecount, int pageid = 1, int pagesize = 20)
        {
            this.PageCount = pagecount;
            this.RecordCount = recordcount;

            this.PageIndex = pageid;
            this.PageSize = pagesize;

            lblPageCount.Text = this.PageCount.ToString();
            lblRecordCount.Text = this.RecordCount.ToString();
            tbPageIndex.Text = this.PageIndex.ToString();
            tbPageSize.Text = this.PageSize.ToString();
            lblPageIndex.Text = this.PageIndex.ToString();
        }

        /// <summary>
        /// 跳转到第一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lnkFirstPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Paging(this.FirstPage, this.PageSize);
        }

        /// <summary>
        /// 跳转到上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lnkPrevPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Paging(this.PrevPage, this.PageSize);
        }

        /// <summary>
        /// 跳转到下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lnkNextPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Paging(this.NextPage, this.PageSize);
        }

        /// <summary>
        /// 跳转到最末页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lnkLastPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Paging(this.LastPage, this.PageSize);
        }

        /// <summary>
        /// 更改分页尺寸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbPageSize_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                int pagesize = tbPageSize.Text.ToInt();
                if (pagesize <= 0) return;
                this.Paging(this.FirstPage, pagesize);
            }
        }

        /// <summary>
        /// 更改当前页码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbPageIndex_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                int pageindex = tbPageIndex.Text.ToInt();
                if (pageindex <= 0) return;
                this.Paging(pageindex, this.PageSize);
            }
        }

        public string PrevPageText { get { return lnkPrevPage.Text; } set { lnkPrevPage.Text = value; } }

        public string NextPageText { get { return lnkNextPage.Text; } set { lnkNextPage.Text = value; } }

        public string FirstPageText { get { return lnkFirstPage.Text; } set { lnkFirstPage.Text = value; } }

        public string LastPageText { get { return lnkLastPage.Text; } set { lnkLastPage.Text = value; } }

        public void SetLayout(string layout)
        {
            if (layout == "mini")
            {
                txt1.Visible = false;
                txt2.Visible = false;
                txt3.Visible = false;
                txt5.Visible = false;
                lblRecordCount.Visible = false;
                tbPageSize.Visible = false;
                tbPageIndex.Visible = false;
                lblPageIndex.Visible = true;
            }
        }
    }
}
